//
//  VVideoUploadToast.m
//  vliour
//
//  Created by huangbolun on 2017/10/16.
//  Copyright © 2017年 huiian. All rights reserved.
//

#import "VVideoUploadToast.h"
@interface VVideoUploadToast ()

@property(nonatomic, strong) UILabel *contentLabel;

@end

@implementation VVideoUploadToast

+ (instancetype)shareToast{
    static VVideoUploadToast *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [VVideoUploadToast new];
    });
    return instance;
}

- (instancetype)init{
    if ([super init]) {
        self.progress = 0;
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
//    self.bounds = CGRectMake(0, 0, 200, 70);
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    self.layer.cornerRadius = 30;
    
    UILabel *contentLabel = [UILabel new];
    self.contentLabel = contentLabel;
    [self addSubview:contentLabel];
    contentLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [[contentLabel.centerXAnchor constraintLessThanOrEqualToAnchor:self.centerXAnchor] setActive:YES];
    [[contentLabel.centerYAnchor constraintLessThanOrEqualToAnchor:self.centerYAnchor] setActive:YES];
    contentLabel.textAlignment = NSTextAlignmentCenter;
    contentLabel.textColor = [UIColor whiteColor];
    contentLabel.text = [NSString stringWithFormat:@"视频上传中 %ld%% \n请勿关闭",self.progress];
    contentLabel.numberOfLines = 0;
    
}

- (void)show{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
//    self.translatesAutoresizingMaskIntoConstraints = NO;
    
    self.frame = CGRectMake(0, 0, 200, 60);
    self.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
}

- (void)dismiss{
    [[VVideoUploadToast shareToast] removeFromSuperview];
    self.progress = 0;
}

- (void)dismissWithTitle:(NSString *)title delayed:(NSTimeInterval)delayed completion:(void (^)())completion{
    self.contentLabel.text = title;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayed * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self dismiss];
        completion();
    });
}

#pragma mark - set
- (void)setProgress:(NSInteger)progress{
    _progress = progress;
    self.contentLabel.text = [NSString stringWithFormat:@"视频上传中 %ld%% \n请勿关闭",self.progress];
}

@end
